thin.imports('/public/configure/conf.bp.js');

thin.use(function() {
	$.showLoading();
	factory.request('_bp_volist');

	checkLocation();

	$('#btn-uploadfile-pdf').on('click', function() {
		var u = navigator.userAgent;
		var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
		//var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
		if(isAndroid) {
			// 安卓用户
			$('#uploadfile-pdf').click();
		} else {
			// iOS用户
			$.alert("请使用电脑登录账号上传商业计划书", "提示");
		}
	});

	// 绑定上传商业计划书按钮
	$('#uploadfile-pdf').on('change', function(e) {
		var src,
			url = window.URL || window.webkitURL || window.mozURL,
			files = e.target.files;
		if(!files[0]) return null;
		if(url) {
			src = url.createObjectURL(files[0]);
		} else {
			src = e.target.result;
		}
		var u = $(this).attr('data-u');
		window[u].get($(this), files[0].name);
		var fileInput = this.files[0];
		var form = new FormData();
		form.append("file", fileInput);
		form.append("name", "file");
		form.append("document", "pdf");
		form.append("dir", 'upload');
		ajax().upload('/api?common.upload', form, window[u]);
		this.parentNode.reset();
	});

	$('#bpv-modal-list').on('click', '#btn-delpdf', function() {
		var _id = $(this).attr('data-id');
		$.confirm("确定删除商业计划书吗", function() {
			//点击确认后的回调函数
			$.showLoading();
			__getdata_bpdelete['id'] = _id;
			factory.request('_bp_delete', _id);
		}, function() {
			//点击取消后的回调函数
		});
	});
});

function __complete_bpvolist(res) {
	$.hideLoading();
	//console.dir(res);
	if(res.status == 200) {
		if(res.data.length > 0) {
			createBpv(res.data);
			$('.nothing-box').hide();
		} else {
			$('#bpv-modal-list').html('');
			$('.nothing-box').show();
		}
	} else {
		$('#bpv-modal-list').html('');
		$('.nothing-box').show();
		$.toptip(res.error, 'error');
	}
}

function createBpv(data) {
	var ele = '';
	for(var i in data) {
		ele += '<div class="weui-cell" data-id="' + data[i].id + '">';
		ele += '<div class="weui-cell__hd m-r-3x">';
		ele += '<i class="icon-files-pdf fs32r c-ff9e80"></i>';
		ele += '</div>';
		ele += '<div class="weui-cell__bd">';
		ele += '<p>' + data[i].title + '</p>';
		ele += '<p class="fs14r">' + tool.formatDate('yy-mm-dd hh:ii', data[i].create_time) + '</p>';
		ele += '</div>';
		ele += '<div class="weui-cell__ft">';
		ele += '<a href="javascript:;" class="m-l-2x" id="btn-delpdf" data-id="' + data[i].id + '">';
		ele += '<i class="icon-delete fs18r"></i>';
		ele += '</a>';
		ele += '<a href="' + data[i].bp_uri + '" class="m-l-2x">';
		ele += '<i class="icon-download fs18r"></i>';
		ele += '</a>';
		ele += '</div>';
		ele += '</div>';
	}
	$('#bpv-modal-list').html(ele);
}

// 删除商业计划书回调
function __complete_bpdelete(res, unkn) {
	$.hideLoading();
	if (res.status == 204) {
		$.toast("删除成功", 960);
		setTimeout(function() {
			$('div[data-id=' + unkn + ']').remove();
		}, 800);
	} else {
		$.toptip(res.error, 'error');
	}
}

var __getdata_bpupload = {},
	__getdata_bpdelete = {};

var __ubp = {
	'get': function(obj, name) {
		__getdata_bpupload['title'] = name;
	},
	'onload': function(res) {
		if(res.status == 201) {
			$.showLoading();
			__getdata_bpupload['bp_uri'] = res.data.uri;
			factory.request('_bp_upload');
		} else {
			$.toptip(res.error, 'error');
		}
	},
	'onerror': function(res) {
		$.toptip('上传失败，请检测图片和网络', 'error');
	}
}

function __complete_bpupload(res) {
	$.hideLoading();
	if(res.status == 201) {
		$.toast("上传成功", 960);
		setTimeout(function() {
			window.location.reload();
		}, 800);
	} else {
		$.toptip(res.error, 'error');
	}
}